var starscore = require("./templates/starscore/starscore.js");
var md5 = require("./utils/md5.js");
const mtjwxsdk = require('./utils/mtj-wx-sdk.js');
App({
  onShow: function (options) {
    var scene = options.scene;
    this.globalData.scene = scene;
    this.globalData.wxPayShopId = options.query.q;
  },
  onLaunch: function(options) {
    var that = this;
    var service = 'User.GetShopId';
    var sign = md5.md5(service);

    var openid = options.query.openid;
    console.log(openid);
    console.log('app打印')
    var user_id = options.query.user_id;
    if (user_id != undefined && openid != undefined) {
      that.globalData.openId = openid
      that.globalData.userId = user_id
      wx.setStorageSync('openId', openid);
      wx.setStorageSync('userId', user_id);
    }

    if (options.query.shop_id == undefined) {
      var sync_shop_id = wx.getStorageSync('shop_id');
      if (sync_shop_id == '') {
        that.globalData.shopId = 1066;
      } else {
        that.globalData.shopId = sync_shop_id;
      }
    } else {
      var shop_id = options.query.shop_id;
      that.globalData.shopId = shop_id;
      wx.setStorageSync('shop_id', shop_id)

    }
    wx.getSystemInfo({
      //获取系统信息成功，将系统窗口的宽高赋给页面的宽高  
      success: function(res) {
        wx.setStorageSync('width', res.windowWidth)
        wx.setStorageSync('height', res.windowHeight - 49)
      }
    })

    that.globalData.openId = wx.getStorageSync('openId') || 0;
    that.globalData.userId = wx.getStorageSync('userId') || 0;
    that.globalData.userTel = wx.getStorageSync('userTel');
    //  获取商城名称
    var open_id = that.globalData.openId;
    var service = 'User.GetMyShop';
    var shop_id = that.globalData.shopId;
    var user_id = that.globalData.userId;
    var sign = md5.md5(open_id + service + shop_id + user_id);

    wx.login({
      success: function(res) {
        wx.request({
          url: that.globalData.openIdUrl,
          data: {
            code: res.code,
            pay_type: 'shop',
          },
          header: {
            "Content-Type": "application/x-www-form-urlencoded"
          },
          success: function(res) {
            that.globalData.openIdPay = res.data.openid;
          },
          complete: function(res) {
            console.log(res);
          },
        })
      }
    })

    wx.request({
      url: that.globalData.serviceUrl + 'User.GetMyShop',
      header: {
        'Content-Type': 'json'
      },
      data: {
        open_id: open_id,
        shop_id: shop_id,
        user_id: user_id,
        sign: sign,
      },
      success: function(res) {
        if (res.data.data.code == 0) {
          wx.setStorageSync('mallName', res.data.data.info[0].shop_name);
          wx.setStorageSync('shoptese', res.data.data.info[0].shop_special);
          wx.setStorageSync('shopTel', res.data.data.info[0].shop_tel);
        }
      },
      complete: function(res) {
        console.log(res);
      },
    })
    var service = 'User.GetUserInfo';
    var sign = md5.md5(open_id + service + user_id);
    wx.request({
      url: that.globalData.serviceUrl + 'User.GetUserInfo',
      header: {
        'Content-Type': 'json'
      },
      data: {
        open_id: open_id,
        user_id: user_id,
        sign: sign,
      },
      success: function(res) {
        if (res.data.data.code == 0) {
          that.globalData.userTel = res.data.data.info[0].tel;
        }
      },
      complete: function(res) {
        console.log(res);
      },
    })
  },

  globalData: {
    scene: 0,
    page: 1, //初始加载商品时的页面号
    pageSize: 10000, //初始加载时的商品数，设置为10000保证小商户能加载完全部商品
    categories: [{
      id: 0,
      name: "热销商品"
    }],
    goods: [],
    hotGoods: [], 
    goodsName: [],
    goodsList: [],
    onLoadStatus: true,
    activeCategoryId: null,
    userInfo: null,
    subDomain: "tianzujie", // 商城后台个性域名tgg
    version: "2.0.6",
    shareProfile: '   一流的服务，做超新鲜的水果', // 首页转发的时候术语

    //serviceUrl: 'http://localhost/hongbao_api/Public/hongbao/?service=',
    serviceUrl: 'https://api.zntech-china.com/Public/hongbao/?service=', //服务地址
    erpUrl: 'https://mylife.zntech-china.com',//ERP服务地址
    // serviceUrl: 'https://wx.zonuo-china.com/hongbao_api/Public/hongbao/?service=',
    // erpUrl:'https://mytest.zonuo-china.com',
    userId: 0, //接口userid
    openId: 0, //接口openid
    shopId: 0, //商铺id
    shopName:'',//店铺名称
    shopPic:'',//店铺logo
    userTel: '', //电话
    openIdUrl: 'https://mylife.zntech-china.com/web/get_wx_open_id', //获取支付openid接口
    openIdPay: '', //支付openid
    payUrl: 'https://mylife.zntech-china.com/web/get_pay_wx', //支付接口
    userAccount: 0,
    pageFlag: false,
    nickname: '', //昵称
    avatar: '', //头像
  }
})